package cn.sxw.ld.common.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * ClassName:RedisUtils
 * Package:cn.sjxy.graduation.common.utils
 * Description:
 *
 * @Date:2023/3/28 19:32
 * @Author:SXW
 */
@Component
public class RedisUtils {

    @Autowired
    private  RedisTemplate redisTemplate;


    /**
     * @param email
     */
    public  void setNum(String email, Object num) {
        BoundValueOperations bvo = redisTemplate.boundValueOps(email + "::num");
        // 写入缓存，并设置过期时间
        bvo.set(num, 24, TimeUnit.HOURS);
    }

    /**
     * 设置到过期
     *
     * @param email
     * @param code
     */
    public  void setRedis(String email, String code) {
        BoundValueOperations bvo = redisTemplate.boundValueOps(email);
        // 写入缓存，并设置过期时间
        bvo.set(code, 5, TimeUnit.MINUTES);
    }

    /**
     * 设置禁用名单
     *
     * @param email
     */
    public  void setRedisBanned(String email) {
        BoundValueOperations bvo = redisTemplate.boundValueOps(email + "::Banned");
        // 写入缓存，并设置过期时间
        bvo.set("banned", 24, TimeUnit.HOURS);
    }

    /**
     * 获取发送次数
     *
     * @param email
     * @return
     */
    public  int getNum(String email) {
        // 获取发送次数
        Object o = redisTemplate.opsForValue().get(email + "::num");
        if (o != null) {
            return Integer.valueOf(o.toString());
        }
        return 1;
    }

    /**
     * 查询是否被禁用
     * 为空返回假，不为空返回真
     *
     * @param email
     * @return
     */
    public  Boolean isBanned(String email) {
        Object o = redisTemplate.opsForValue().get(email + "::Banned");
        if (o == null) {
            return false;
        }
        return true;
    }

    /**
     * @param email
     */
    public  Object getCode(String email) {
        return redisTemplate.opsForValue().get(email);
    }
}
